<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!--      <el-form-item label="日期筛选" label-width="100">-->
      <!--        <el-date-picker-->
      <!--          v-model="dateRange"-->
      <!--          value-format="YYYY-MM-DD HH:mm:ss"-->
      <!--          type="daterange"-->
      <!--          range-separator="-"-->
      <!--          start-placeholder="开始日期"-->
      <!--          end-placeholder="结束日期"-->
      <!--          :default-time="[-->
      <!--            new Date(2000, 1, 1, 0, 0, 0),-->
      <!--            new Date(2000, 1, 1, 23, 59, 59),-->
      <!--          ]"-->
      <!--        ></el-date-picker>-->
      <!--      </el-form-item>-->

      <el-form-item label="用户ID" prop="userId" label-width="100">
        <el-input
          v-model="queryParams.userId"
          placeholder="用户ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="用户昵称" prop="nickName" label-width="100">
        <el-input
          v-model="queryParams.nickName"
          placeholder="用户昵称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <!--      <el-form-item label="老师" prop="teacherName" label-width="100">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.teacherName"-->
      <!--          placeholder="老师"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->

      <!--      <el-form-item label="会员卡号" prop="membershipCardNumber" label-width="100">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.membershipCardNumber"-->
      <!--          placeholder="会员卡号"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="客户姓名" prop="membershipName" label-width="100">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.membershipName"-->
      <!--          placeholder="客户姓名"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="是否扣钱" prop="isDeductMoney" label-width="100">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.isDeductMoney"-->
      <!--          placeholder="是否扣钱"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="扣钱状态" prop="withholdingStatus" label-width="100">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.withholdingStatus"-->
      <!--          placeholder="扣钱状态"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['business:hairTriggerEvent:export']"
        >导出
        </el-button>
        <el-button
          type="warning"
          plain
          @click="giveAwayHandle"
        >赠送荣誉勋章
        </el-button>
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <right-toolbar-->
    <!--        v-model:showSearch="showSearch"-->
    <!--        @queryTable="getList"-->
    <!--      ></right-toolbar>-->
    <!--    </el-row>-->

    <el-table
      v-loading="loading"
      :data="hairTriggerEventList"
    >
      <el-table-column label="用户id" align="center" prop="userId"/>
      <el-table-column label="用户昵称" align="center" prop="nickName"/>
      <el-table-column label="任务勋章进度" align="center">
        <template #default="scope">
          <span>{{ scope.row.taskMedalAchieveNum }} / {{ scope.row.taskMedalTotalNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="荣誉勋章进度" align="center" prop="militaryArea">
        <template #default="scope">
          <span>{{ scope.row.honorMedalAchieveNum }} / {{ scope.row.honorMedalTotalNum }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" width="260" label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button type="primary" @click="handleDetails(scope.row)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="赠送荣誉勋章" v-model="giveOpen" width="500px" append-to-body>
      <el-form ref="medalUserGiveRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="选择勋章" prop="medalKey">
          <el-select clearable v-model="form.medalKey" placeholder="请选择">
            <el-option
              v-for="dict in medalConfigList"
              :key="dict.id"
              :label="dict.medalName"
              :value="dict.medalKey"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <h2>任务勋章进度</h2>
      <el-table :data="missionMedalList" width="400">
        <el-table-column property="medalName" label="勋章名称"></el-table-column>
        <el-table-column label="点亮状态" width="200">
          <template #default="scope">
            <span>{{ scope.row.lightStatus ? "已点亮" : "未点亮" }} </span>
          </template>
        </el-table-column>
        <el-table-column label="等级" width="200" prop="gradeNum">
        </el-table-column>
        <el-table-column label="进度" width="200">
          <template #default="scope">
            <span>{{ scope.row.currentPromotionProgress }} / {{ scope.row.lightProgress }}</span>
          </template>
        </el-table-column>
      </el-table>
      <h2>荣誉勋章进度</h2>
      <el-table :data="badgeMedalList">
        <el-table-column property="medalName" label="勋章名称"></el-table-column>
        <el-table-column label="点亮状态" width="200">
          <template #default="scope">
            <span>{{ scope.row.lightStatus ? "已点亮" : "未点亮" }} </span>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

  </div>
</template>

<script setup name="HairTriggerEvent">
import {
  getUserMedalProgress,getUserMedalProgressDetails
} from "@/api/business/operation";
import { listMedalConfig } from "@/api/business/medalConfig";
import {useRoute, useRouter} from "vue-router";
import {getUser} from "@/api/system/user";
import {addMedalUserGive} from "@/api/business/medalUserGive";

const router = useRouter();
const route = useRoute()
const {proxy} = getCurrentInstance();

const hairTriggerEventList = ref([]);
const hairTaskDetails = ref([]);
// 任务勋章list
const missionMedalList = ref([]);
// 荣誉勋章list
const badgeMedalList = ref([]);
const medalConfigList = ref([]);
const open = ref(false);
const giveOpen = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
// const dateRange = ref([proxy.parseTime(new Date(), "{y}-{m}-{d}") + ' 00:00:00' ,proxy.parseTime(new Date(), "{y}-{m}-{d}") + ' 23:59:59']);
const dateRange = ref([]);
const validateUser = (rule, value, callback) => {
  getUser(value).then((response) => {
    if(response.data.user == null){
      callback(new Error('识别状态：该用户不存在'));
    }else {
      callback();
    }
  });
};
const data = reactive({

  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 15,
    nickName: undefined,
    salesDirector: undefined,
    eventValue: undefined,
  },
  rules: {
    userId: [
      { required: true, message: "用户id不能为空", trigger: "blur" },
      {
        validator: validateUser,
        trigger: "blur",
      },
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询用户触发事件记录列表 */
function getList() {
  loading.value = true;
  getUserMedalProgress(proxy.addDateRange(queryParams.value, dateRange.value)).then((response) => {
    hairTriggerEventList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
  listMedalConfig({medalType: "2"}).then(response => {
    medalConfigList.value = response.rows;
  });
}
// 取消按钮
function cancel() {
  giveOpen.value = false;
  reset();
}

function submitForm(){
  addMedalUserGive(form.value).then(response => {
    proxy.$modal.msgSuccess("赠送成功");
    giveOpen.value = false;
    form.value = {};
    getList();
  }).finally(() => {
    buttonLoading.value = false;
  });
}


/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function handleDetails(row) {
  open.value = true;
  getUserMedalProgressDetails(row.userId,"1").then(res => {
    missionMedalList.value = res.data
  })
  getUserMedalProgressDetails(row.userId,"2").then(res => {
    badgeMedalList.value = res.data
  })
}

function giveAwayHandle(){
  giveOpen.value = true
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  queryParams.value = {
    pageNum: 1,
    pageSize: 15,
    userId: null,
    type: null,
    startDate: null,
    endDate: null,
    nickName: null,
    salesDirector: null,
    eventValue: null,
    userType: null
  };
  handleQuery();
}


/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "business/operation/exportUserTaskProgress",
    {
      ...queryParams.value,
    },
    `accountStatement_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
